#-*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.loader import get_template
from django.template import RequestContext

from google.appengine.ext import db

from common.view_decorator import role_required
from models.main import App_user, Post, Comment, Tag, Sidebar
from smart_pager import Pager
import datetime

from common.time_utils import parse_time

@role_required('admin')
def index(request):
    return render_to_response('admin/index.html', {

        }, RequestContext(request))

@role_required('admin')
def left(request):
    return render_to_response('admin/left.html', {

        }, RequestContext(request))

@role_required('admin')
def add_post(request):
    return render_to_response('admin/add_post.html', {

        }, RequestContext(request))

@role_required('admin')
def add_post_post(request):
    title = request.POST['title'].strip()
    content = request.POST['content'].strip()
    sep = '<hr class="more">'
    sep_index = content.find(sep)
    if sep_index != -1:
        summary = content[0:sep_index]
    else:
        summary = None
    
    add_time_date = request.POST.get('add_time_date').strip()
    add_time_time = request.POST.get('add_time_time').strip()
    if add_time_date and add_time_time:
        add_time = parse_time(add_time_date + ' '+ add_time_time)
    else:
        add_time = None
    Post.add(title, content, summary = summary, add_time = add_time)

    return HttpResponse('ok')

@role_required('admin')
def post_list(request):
    page = int(request.GET.get('page', 1))
    if page < 1:
        page = 1
    page_size = 15
    total = Post.count()
    post_list = Post.get_list(page, page_size)

    pager = Pager(total, page_size, page, page_list_num = 10)

    return render_to_response('admin/post_list.html', {
            'post_list': post_list,
            'pager': pager,
        }, RequestContext(request))

@role_required('admin')
def del_post(request):
    post_key = request.POST['post_key']
    post = db.get(post_key)
    post.delete()

    return HttpResponse('ok')

@role_required('admin')
def edit_post(request):
    post_key = request.GET['post_key']
    post = db.get(post_key)

    return render_to_response('admin/edit_post.html', {
            'post': post,
        }, RequestContext(request))

@role_required('admin')
def edit_post_post(request):
    post_key = request.GET['post_key']
    post = db.get(post_key)

    title = request.POST['title'].strip()
    content = request.POST['content'].strip()
    sep = '<hr class="more">'
    sep_index = content.find(sep)
    if sep_index != -1:
        summary = content[0:sep_index]
    else:
        summary = None
    
    add_time_date = request.POST.get('add_time_date').strip()
    add_time_time = request.POST.get('add_time_time').strip()
    if add_time_date and add_time_time:
        add_time = parse_time(add_time_date + ' '+ add_time_time)
    else:
        add_time = None
    
    post.modify(title, content, summary = summary, add_time = add_time)

    return HttpResponse('ok')

@role_required('admin')
def comment_list(request):
    page = int(request.GET.get('page', 1))
    if page < 1:
        page = 1

    comment_list = Comment.all().order('-add_time').fetch(150)


    return render_to_response('admin/comment_list.html', {
            'comment_list': comment_list,
        }, RequestContext(request))

@role_required('admin')
def del_comment(request):
    commnet_key = request.POST['commnet_key']
    comment = db.get(commnet_key)
    comment.delete()

    return HttpResponse('ok')

@role_required('admin')
def user_list(request):
    page = int(request.GET.get('page', 1))
    if page < 1:
        page = 1

    user_list = App_user.all().order('-add_time').fetch(150)


    return render_to_response('admin/user_list.html', {
            'user_list': user_list,
        }, RequestContext(request))

@role_required('admin')
def sidebar_list(request):
    sidebar_list = Sidebar.all().order('-add_time').fetch(100)


    return render_to_response('admin/sidebar_list.html', {
            'sidebar_list': sidebar_list,
        }, RequestContext(request))

@role_required('admin')
def sidebar_add(request):
    return render_to_response('admin/sidebar_add.html', {

        }, RequestContext(request))

@role_required('admin')
def sidebar_edit(request):
    item_key = request.GET['item_key']
    sidebar = db.get(item_key)
    return render_to_response('admin/sidebar_edit.html', {
            'sidebar': sidebar
        }, RequestContext(request))

@role_required('admin')
def sidebar_edit_post(request):
    title = request.POST['title']
    content = request.POST['content']
    order = int(request.POST['order'])

    item_key = request.GET['item_key']
    sidebar = db.get(item_key)
    sidebar.title = title
    sidebar.content = content
    sidebar.order = order
    sidebar.put()

    return HttpResponseRedirect('/admin/sidebar_list/')

@role_required('admin')
def sidebar_delete(request):
    item_key = request.POST['item_key']
    sidebar = db.get(item_key)
    sidebar.delete()

    return HttpResponse('ok')

@role_required('admin')
def sidebar_add_post(request):
    title = request.POST['title']
    content = request.POST['content']
    order = int(request.POST['order'])
    
    s = Sidebar(title = title, content = content, order = order)
    s.put()

    return HttpResponseRedirect('/admin/sidebar_list/')
